package net.milkdrops;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;


public class CleanRes {
	private ArrayList<File> resourcesList = null;
	
	private String rootPath;
	private boolean bCancel = false;
	
	public void setbCancel(boolean bCancel) {
		this.bCancel = bCancel;
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	private CleanResGUI callback = null;
	
	public CleanRes(String rootPath) {
		super();
		this.rootPath = rootPath;
	}
	
	@Override
	protected void finalize() throws Throwable {
		if (resourcesList != null) {
			resourcesList.clear();
		}
		super.finalize();
	}
	
	private static String getFileName(File file) {
		String fileFullName = file.getName();
		if (fileFullName != null && fileFullName.indexOf(".") > 0) {
			return fileFullName.substring(0,
					fileFullName.indexOf("."));
		}
		return "";
	}

	public void removeResources(CleanResGUI callback) {
		bCancel = false;
		if (callback != null) {
			this.callback = callback;
		}
		if (resourcesList != null) {
			resourcesList.clear();
		}
		resourcesList = new ArrayList<File>();
		searchResources();
		if (bCancel) {
			return;
		}
		checkSrcUsage();
		if (bCancel) {
			return;
		}
		checkResUsage();
		if (bCancel) {
			return;
		}
		showResult();
	}
	
	public void searchResources() {
		File resRoot = new File(rootPath+"/res");
		
		for (File dir : resRoot.listFiles(new FileFilter() {

			@Override
			public boolean accept(File arg0) {
				if (arg0.getPath().contains("drawable")) {
					return true;
				}
				return false;
			}
			
		})) {
			if (bCancel) {
				return;
			}
			if (!dir.isDirectory()) {
				continue;
			}
			for (File file : dir.listFiles()) {
				if (!resourcesList.contains(file)) {
					resourcesList.add(file);
				}
			}
		}
	}
	
	public static void addAllFiles(LinkedList<File> list, File src) {
		for (File file : src.listFiles()) {
			list.addLast(file);
		}
	}
	
	public static void addAllFiles(LinkedList<File> list, File src, FilenameFilter filter) {
		for (File file : src.listFiles(filter)) {
			list.addLast(file);
		}
	}
	
	public void removeUsingResources(String line) {
		LinkedList<File> remove = new LinkedList<File>();
		for (File resource : resourcesList) {
			String name = getFileName(resource);
			if (!remove.contains(name) && line.contains(name)) {
				remove.add(resource);
			}
		}
		resourcesList.removeAll(remove);
		remove.clear();
	}
	
	public void checkSrcUsage() {
		File srcRoot = new File(rootPath+"/src");
	
		LinkedList<File> fileList = new LinkedList<File>();
		
		FilenameFilter filter = new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				if (name.contains(".svn")) {
					return false;
				}
				return true;
			}
			
		};
		
		addAllFiles(fileList, srcRoot, filter);
		while (!fileList.isEmpty()) {
			if (bCancel) {
				return;
			}
			File file = fileList.removeFirst();
			if (file.isDirectory()) {
				addAllFiles(fileList, file, filter);
				continue;
			}
			
			if (callback != null) {
				callback.setProgress(file);
			}
			
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				String line;
				while((line = reader.readLine()) != null) {
					removeUsingResources(line);
				}
				reader.close();
			//	System.out.println("src="+file.getAbsolutePath());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void checkResUsage() {
		File resRoot = new File(rootPath+"/res");
	
		LinkedList<File> fileList = new LinkedList<File>();
		
		FilenameFilter filter = new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				if (name.contains(".svn")) {
					return false;
				}
				if (dir.isFile() && name.contains("xml")) {
					return true;
				} else if (dir.isDirectory()) {
					return true;
				}
				return false;
			}
			
		};
		
		addAllFiles(fileList, resRoot, filter);
		
		while (!fileList.isEmpty()) {
			if (bCancel) {
				return;
			}
			File file = fileList.removeFirst();
			if (file.isDirectory()) {
				addAllFiles(fileList, file, filter);
				continue;
			}
			
			if (callback != null) {
				callback.setProgress(file);
			}
			
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				String line;
				while((line = reader.readLine()) != null) {
					removeUsingResources(line);
				}
				reader.close();
				//System.out.println("res="+file.getAbsolutePath());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void deleteAll() {
		LinkedList<File> remove = new LinkedList<File>();
		for (File resource : resourcesList) {
			resource.delete();
			remove.add(resource);
		}
		resourcesList.removeAll(remove);
	}
	
	public void delete(int[] list) {
		LinkedList<File> remove = new LinkedList<File>();
		for (int i : list) {
			File file = resourcesList.get(i);
			file.delete();
			remove.add(file);
		}
		resourcesList.removeAll(remove);
	}
	
	public void showResult() {
		if (callback != null) {
			callback.setResult(resourcesList);
		} else {
			System.out.println("========================");
			for (File resource : resourcesList) {
				String name = getFileName(resource);
				System.out.println(name);
			}
			System.out.println("========================");
		}
	}
	
	public String[] getResult() {
		String[] result = new String[resourcesList.size()];
		for (int i=0; i<resourcesList.size(); i++) {
			result[i] = resourcesList.get(i).getName();
		}
		return result;
	}
}
